/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.Driver</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class Drivers {

    /**
     * No instance of this class.
     */
    private Drivers() {
    } // end empty constructor

    /**
     * Tests whether the passed driver accepts a given url.
     * @param ctxt context
     * @param driver driver
     * @param url url to test
     * @return <tt>true</tt> if the passed driver accepts the url,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_driver_accepts_url(final CodeRunner ctxt,
                                                       final Value driver,
                                                       final Value url)
        throws Fail.Exception {
        try {
            final Driver drv = (Driver) driver.asBlock().asCustom();
            return drv.acceptsURL(url.asBlock().asString())
                ? Value.TRUE
                : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_driver_accepts_url(CodeRunner, Value, Value)'

    /**
     * Connects to a database.
     * @param ctxt context
     * @param driver driver
     * @param url database url
     * @param info properties passed to driver for connection creation
     * @return a new connection
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_driver_connect(final CodeRunner ctxt,
                                                   final Value driver,
                                                   final Value url,
                                                   final Value info)
        throws Fail.Exception {
        try {
            final Driver drv = (Driver) driver.asBlock().asCustom();
            final Connection res = drv.connect(url.asBlock().asString(),
                                               Misc.decodeProperties(info));
            return Cadmium.createObject(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_driver_connect(CodeRunner, Value, Value, Value)'

    /**
     * Returns the major component of the passed driver.
     * @param ctxt context
     * @param driver driver
     * @return the major component of the passed driver
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_driver_get_major_version(final CodeRunner ctxt,
                                                             final Value driver)
        throws Fail.Exception {
        try {
            final Driver drv = (Driver) driver.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_32_SIZE,
                                                 Custom.INT_32_OPS);
            res.setInt32(drv.getMajorVersion());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_driver_get_major_version(CodeRunner, Value)'

    /**
     * Returns the minor component of the passed driver.
     * @param ctxt context
     * @param driver driver
     * @return the minor component of the passed driver
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_driver_get_minor_version(final CodeRunner ctxt,
                                                             final Value driver)
        throws Fail.Exception {
        try {
            final Driver drv = (Driver) driver.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_32_SIZE,
                                                 Custom.INT_32_OPS);
            res.setInt32(drv.getMinorVersion());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_driver_get_minor_version(CodeRunner, Value)'

    /**
     * Returns the possible properties for the passed driver.
     * @param ctxt context
     * @param driver driver
     * @param url url of database
     * @param info proposed properties
     * @return the possible properties for the passed driver
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_driver_get_property_info(final CodeRunner ctxt,
                                                             final Value driver,
                                                             final Value url,
                                                             final Value info)
        throws Fail.Exception {
        try {
            final Driver drv = (Driver) driver.asBlock().asCustom();
            final DriverPropertyInfo[] infos =
                drv.getPropertyInfo(url.asBlock().asString(),
                                    Misc.decodeProperties(info));
            Value res = Value.EMPTY_LIST;
            for (DriverPropertyInfo dpi : infos) {
                final Value v = Misc.encodeDriverPropertyInfo(dpi);
                final Block cons = Block.createBlock(Block.TAG_CONS, v, res);
                res = Value.createFromBlock(cons);
            } // end for
            return res;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_driver_get_property_info(CodeRunner, Value, Value, Value)'

    /**
     * Tests whether the passed driver is JDBC compliant.
     * @param ctxt context
     * @param driver driver
     * @return <tt>true</tt> if the passed driver is JDBC compliant,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_driver_is_jdbc_compliant(final CodeRunner ctxt,
                                                             final Value driver)
        throws Fail.Exception {
        try {
            final Driver drv = (Driver) driver.asBlock().asCustom();
            return drv.jdbcCompliant() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_driver_is_jdbc_compliant(CodeRunner, Value)'

} // end class 'Drivers'
